<?php
/**
 * 会员管理控制器
 * User: Administrator
 * Date: 2018/10/29 0029
 * Time: 16:07
 */
namespace app\admin\controller;

use app\admin\model\ScoreLog;
use app\admin\model\SystemUser;
use service\DataService;
use think\Db;
use think\Hook;

class Member extends BasicAdmin
{

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'ml_member';

    /**
     * 会员列表
     */
    public function index(){

        //$this->request->input();

        if($this->request->isPost()){
            $param = $this->request->param();
            $db = Db::name($this->table)->order('id '.$param['order']);
            foreach (['username', 'nickname'] as $key) {
                (isset($param[$key]) && $param[$key] !== '') && $db->whereLike($key, "%{$param[$key]}%");
            }
            (isset($param['status']) && $param['status'] !== '') && $db->where('status',$param['status']);
            return parent::_list($db,$param);
        }
        $this->title = '会员管理 > 用户列表';
        return $this->fetch('', ['title' => $this->title]);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data)
    {
        foreach ($data as &$vo) {
            $vo['area'] = $vo['province'].' '.$vo['city'];
            $vo['birthday'] = format_datetime($vo['birthday'],'Y年m月d日');
        }
    }

    /**
     * 会员详情
     */
    public function info(){
        return $this->_form($this->table, 'info');
    }

    /**
     * 详情数据处理
     * @param array $data
     */
    protected function _info_form_filter(&$data){
        $data['scorelog_lists'] = ScoreLog::where('uid',$data['id'])->order('id desc')->select();
    }

    /**
     * 用户状态（1启用 2禁用）
     */
    public function status(){

        $status = $this->request->param('value');
        if(2 == $status){
            if (in_array('1', explode(',', $this->request->post('id')))){
                $this->result([],0,'系统超级账号禁止操作！','json');
            }
        }
        if (DataService::update($this->table)) {
            $msg = $status == 1 ? '用户启用成功！':'用户禁用成功！';
            $this->result(['status'=>$status],200,$msg,'json');
        }
        $msg = $status == 1 ? '用户启用失败，请稍候再试！':'用户禁用失败，请稍候再试！';
        $f_status = $status == 1 ? 2 : 1;
        $this->result(['status'=>$f_status],0,$msg,'json');
    }

    /**
     * 积分充值
     */
    public function integralRecharge(){

        if($this->request->isPost()){
            $param = array_filter($this->request->post());
            if(!is_numeric($param['integral']) || $param['integral'] < 0){
                $this->result([],'0','积分数值不正确','json');
            }
            $integral = \app\admin\model\Member::where('id',$param['id'])->value('integral');
            $param['integral'] += $integral;
            if($rst = DataService::save(Db::name($this->table),$param)){
                //积分明细
                $data = $param;
                $data['integral'] = $this->request->post('integral','','trim');
                $data['from'] = '系统赠送';
                Hook::exec('hook\\Score','scoreLog',$data);
            }
            $msg = doReturn('恭喜, 数据保存成功!','数据保存失败, 请稍候再试!',$rst,$param);
            $this->resultArr($msg);
        }
        return $this->_form($this->table, 'integral_recharge');
    }
}